import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

function checkLogin() {
    return true
}

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};
const routes = [
    {
        path: '/', name: 'home', component: () => import('../views/Home.vue')
    },
    {
        path: '/admin', name: 'admin', component: () => import('../views/Admin.vue'),
        redirect: '/list',
        children: [
            {
                path: '/list', name: 'list', component: () => import('../views/container/List.vue')
            },
            {
                path: '/community', name: 'community', component: () => import('../views/container/Community.vue')
            },
            {
                path: '/adv_admin', name: 'adv_admin', component: () => import('../views/container/Adv_admin.vue')
            },
        ]
    },
    {
        path: '/login', name: 'login', component: () => import('../views/Login.vue')
    },
    {
        path: '/adv_yunnan', name: 'adv_yunnan', component: () => import('../views/Adv_yunnan.vue')
    }
];

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
});

router.beforeEach((to, from, next) => {
    // console.log(to, from, next)
    if (checkLogin() || to.path === '/' || to.path === '/login') {
        next()
    } else {
        next('/login');
        sessionStorage.clear();
        localStorage.clear();
    }
});

export default router
